package com.example.student.controller;

import com.example.student.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public String login(){
        return "login";
    }

    @PostMapping("/login")
    public String login(String username,String password){
        if (userService.isExist(username,password)){
            return "loginSuccess";
        }else {
            return "login";
        }
    }

    @GetMapping("/register")
    public String register(){
        return "register";
    }

    @PostMapping("/register")
    public String register(String username,String pwd){
        if (userService.findByUsername(username)==0){
            userService.register(username,pwd);
            return "login";
        }else {
            return "register";
        }
    }
}
